package com.flca.frw.config;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Use this fileter if you want to filter for a given filename without wildcards
 * @author nly36776
 *
 */
public class FileNameFilter implements FileFilter {

	private String fname;
	private Pattern pattern;
	
	public FileNameFilter(String aFname) {
		super();
		this.fname = aFname;
		this.pattern = buildPattern();
	}
	
	private Pattern buildPattern() {
		String tokens[] = (fname + ".").split("\\.");
		String pstring = (tokens.length == 2) ? "(.*)" + tokens[0] + "\\." + tokens[1] + "$" 
											  : "(.*)" + tokens[0] + "$";
		return Pattern.compile(pstring);
	}

	@Override
	public boolean accept(File pathname) {
		if (pathname.isDirectory()) {
			return true;
		} else {
			Matcher m = pattern.matcher(pathname.getPath());
			boolean b = m.matches();
			if (b) {
				System.out.println(pathname);
			}
			return b;
		}
	}
	
}
